package com.huike.business.service.impl;

import com.huike.business.domain.TbBusinessRecord;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessRecordMapper;
import com.huike.business.service.BusinessRecord;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.common.core.domain.entity.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessRecordImpl implements BusinessRecord {

    @Autowired
    private TbBusinessRecordMapper tbBusinessRecordMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Override
    public List<TbBusinessRecord> findList(Long businessId) {
        List<TbBusinessRecord> list = tbBusinessRecordMapper.findList(businessId);
        list.stream().map(bus -> {
            List<String> keys = bus.getKeys();
            String[] split = bus.getKeyItems().split(",");
            if (split.length == 0) {
                return bus;
            }
            for (int i = 0; i < split.length; i++) {
                String value = sysDictDataMapper.selectDictLabel("reasons_for_reporting", split[i]);
                keys.add(value);
            }
            bus.setKeys(keys);
            return bus;
        });
        return list;

    }

    @Override
    public Integer insertBusinessRecord(TbBusinessRecord tbBusinessRecord) {
        tbBusinessRecordMapper.insertBusinessRecord(tbBusinessRecord);
        Long businessId = tbBusinessRecord.getBusinessId();
        tbBusinessRecord.setId(businessId);
        return tbBusinessMapper.updateTbBusiness(tbBusinessRecord);

    }
}
